/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2003-2011,  Jin Chen and Alan M. MacEachren, GeoVISTA Center, Penn State University
 * Licensed under Simplified BSD License
 *
 *
 * @author: jin Chen 
 * @date: May 13, 2005$
 * @version: 1.0
 */
package edu.psu.geovista.app.pcp.plot.control.actions;

import edu.psu.geovista.app.pcp.DfPcp;
import edu.psu.geovista.app.pcp.Pcp;
import edu.psu.geovista.app.pcp.plot.gui.BasicPcpUI;
import edu.psu.geovista.app.pcp.plot.services.DfCorrelationResult;
import edu.psu.geovista.app.pcp.plot.services.ExternalService;
import edu.psu.geovista.app.pcp.toolkits.collection.CollectionUtils;
import edu.psu.geovista.app.plot.axis.AxisList;
import edu.psu.geovista.data.model.AppDataModel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;



public class SortOnCorrelation extends BasicAction{
    public void actionPerformed(ActionEvent e) {
        Pcp pcp = this.getPcpUI().getPlot();
        ExternalService exsv = pcp.getExternalService();
        AppDataModel data ;
        if(exsv!=null) {

            data = exsv.getExternalData();
        }
        else{
            data=pcp.getDataModel() ;
        }
        String[] columnNames = data.getColumnNames();

        String varname = whatVariableToCorrelation(pcp,columnNames);
        if(varname==null||varname.equals("cancel") ) return;

        //currently valid axis ( in most case, they are visible )
        AxisList yaxisList = pcp.getYaxisList();
        List axisNameList = yaxisList.getAxisNameList();
        String[] axisnames = CollectionUtils.convertToStringArray(axisNameList);


        List<DfCorrelationResult> correlations=null;
        if (exsv!=null) {
            correlations = exsv.getCorrelations(varname,axisnames );
        } else {
            if(pcp instanceof DfPcp){//for now only support Pcp, not time series or other subclass
                BasicPcpUI ui = (BasicPcpUI) ((DfPcp)pcp).getUI();
                correlations=ui.getService().getCorrelations(varname,axisnames,data);

            }

        }
        if(correlations==null)return;


        Collections.sort(correlations);

        int i=0;
        for (Iterator iterator = correlations.iterator(); iterator.hasNext();) {
            DfCorrelationResult rs = (DfCorrelationResult) iterator.next();
            String axisname = rs.getSecondVariable();     //see ServiceManager.getCorrelations() to determine if use primary or second variable
            axisnames[i++]=axisname;
        }

        pcp.setValidYAxes(axisnames);



    }

     String whatVariableToCorrelation(Pcp pcp, String[] columnNames ){
         //dialog component
                JList list=new JList(columnNames);
                JScrollPane scp=new JScrollPane(list);
                String text="Select a variable to compute correlations. Sort will be based on the correlation values.";
                //MultilinesLabel label=new MultilinesLabel();
                JLabel label=new JLabel();
                label.setText(text);
                JPanel pnl=new JPanel();
                pnl.setLayout(new BorderLayout());
                pnl.add(label,BorderLayout.NORTH );
                pnl.add(scp);


                Object[] btns=new String[]{"Sort","Cancel"};
                int value=JOptionPane.showOptionDialog(((JComponent)pcp),pnl,"Please select a variable",
                        JOptionPane.YES_NO_OPTION,
                        JOptionPane.PLAIN_MESSAGE ,
                        null,
                        btns,
                        btns[0]);
                if(value==JOptionPane.OK_OPTION ){
                    //JOptionPane.OK_OPTION map to  "Summary" button
                    String selectedValue = (String) list.getSelectedValue();
                    System.out.println("value:"+selectedValue );
                    return  selectedValue;

                }
                else if(value==JOptionPane.NO_OPTION||value==JOptionPane.CLOSED_OPTION ){
                    //JOptionPane.NO_OPTION map to "Cancel" button
                    System.out.println("cancel");
                    return "cancel";
                }
              return null;
     }
}
